package com.red.wood.module.task;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ShellTask {

	public static void main(String[] args) {
		String shellComm = "echo ====begin start=====\r\n echo hello world";

		ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", shellComm);
		//ProcessBuilder pb = new ProcessBuilder("cmd", "/c", shellComm);
		//是否合并标准错误和标准输出
		pb.redirectErrorStream(true);
		Process process = null;
		try {
			process = pb.start();
			// 执行命令
			BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
			String s = "";
			while ((s = br.readLine()) != null) {
				// 打印信息
				System.out.println(s);
			}
			br.close();
		} catch (IOException e) {
			System.out.println(e);
		} finally {
			// 显式将process.destroy()掉，防止消耗资源
			if (null != process) {
				process.destroy();
			}
		}
	}
}
